



//
//  LYPublicClassesTableViewController.m
//  artist
//
//  Created by laotang on 16/4/17.
//  Copyright © 2016年 viviyoo. All rights reserved.
//

#import "LYPublicClassesTableViewController.h"
#import "LYSelectedClassesViewController.h"
#import "NSDate+Utils.h"
#import "LYSelectedClassInfoTableViewCell.h"
#import "LYCityDataDBTool.h"
#import "LYVideoModel.h"
#import "LYCityDataModel.h"
#import "LYSubjectSelectedViewController.h"
static NSString *const LYPublicClassesCellID= @"LYPublicClassesCellID";
@interface LYPublicClassesTableViewController ()<UIPickerViewDelegate, UIPickerViewDataSource>

@property (nonatomic, strong) UIDatePicker *datePicker;
@property (nonatomic, strong) UIPickerView *pickerView;
@property (nonatomic, assign) CGFloat datePickerHeight;
@property (nonatomic, strong) NSIndexPath * selectedIndexPath;
@property (nonatomic, strong) NSDate * firstDate;
@property (nonatomic, strong) NSDate * secondDate;
@property (nonatomic, strong) NSArray * Provinces;
@property (nonatomic, strong) LYSubjectUploadModel *uploadModel;
@property (nonatomic, strong) NSArray <LYCityDataModel *>*cityDataModel;


@property (nonatomic, strong) NSString *selectedProvince;
@property (nonatomic, strong) NSString *selectedCity;
@property (nonatomic, strong) NSString *selectedArea;
@property (nonatomic, strong) NSString *selectedAreaId;

@end

@implementation LYPublicClassesTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.tableFooterView = [[UIView alloc]init];
    [self.view addSubview:self.datePicker];
    [self.view addSubview:self.pickerView];
    self.datePickerHeight = 200;
    self.title = @"课程信息";
    self.uploadModel = [[LYSubjectUploadModel alloc] init];
    UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithTitle:@"下一步" style:UIBarButtonItemStylePlain target:self action:@selector(nextStep)];
    self.navigationItem.rightBarButtonItem = button;

        [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([LYSelectedClassInfoTableViewCell class]) bundle:nil]forCellReuseIdentifier:LYPublicClassesCellID];
    
    self.cityDataModel = [LYCityDataDBTool getOpenCityData];
    self.selectedSubjects = [NSMutableArray array];
    
}

- (void)nextStep{
    
    NSArray * cells = [self.tableView visibleCells];
    LYSelectedClassInfoTableViewCell * cell0 = cells[0];
    
    self.uploadModel.start_date = cell0.textFeild.text;
    
    if (!self.uploadModel.start_date.length){
        [MBProgressHUD showError:@"请选择开始时间"];
        return;
    }
    LYSelectedClassInfoTableViewCell * cell1 = cells[1];
    self.uploadModel.end_date = cell1.textFeild.text;
    if (!self.uploadModel.end_date.length){
        [MBProgressHUD showError:@"请选择结束时间"];
        return;
    }
    LYSelectedClassInfoTableViewCell * cell2 = cells[2];
    self.uploadModel.city_id = cell2.textFeild.text;
    if (!self.uploadModel.city_id.length){
        [MBProgressHUD showError:@"请选择上课时间"];
        return;
    }
    LYSelectedClassInfoTableViewCell * cell3 = cells[3];
    self.uploadModel.address = cell3.textFeild.text;
    if (!self.uploadModel.address.length){
        [MBProgressHUD showError:@"请填写详细地址"];
        return;
    }
    LYSelectedClassInfoTableViewCell * cell4 = cells[4];
    self.uploadModel.price = cell4.textFeild.text;
    if ([self.uploadModel.price floatValue] <= 0){
        [MBProgressHUD showError:@"课程价格要大于0"];
        return;
    }

    
    NSDate *startDate =  [NSDate dateFromString:cell0.textFeild.text withFormat:@"yyyy-MM-dd"];
    NSDate *endDate = [NSDate dateFromString:cell1.textFeild.text withFormat:@"yyyy-MM-dd"];
    
    
    if ([endDate compare:startDate] == NSOrderedAscending){
        [MBProgressHUD showError:@"结束时间必须大于等于开始时间"];
        return;
    }

    
   self.uploadModel.city_id  = [NSString stringWithFormat:@"%zd", [LYCityDataDBTool getIDWithName:self.selectedCity]];
   self.uploadModel.province_id = [NSString stringWithFormat:@"%zd", [LYCityDataDBTool getIDWithName:self.selectedProvince]];
   self.uploadModel.area_id = [NSString stringWithFormat:@"%zd", [LYCityDataDBTool getIDWithName:self.selectedAreaId]];

    LYSubjectSelectedViewController *selectVC = [[LYSubjectSelectedViewController alloc] init];
    selectVC.uploadModel = self.uploadModel;
    

    [self.navigationController pushViewController:selectVC animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 5;
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
   LYSelectedClassInfoTableViewCell * cell = (LYSelectedClassInfoTableViewCell *)[tableView dequeueReusableCellWithIdentifier:LYPublicClassesCellID];
    if (cell == nil ){
        
    }
    switch (indexPath.row) {
        case 0:
            cell.titleLabel.text = @"课程开始时间";
            cell.placeHoldImageView.hidden = YES;
            cell.textFeild.hidden = YES;
            cell.textFeild.userInteractionEnabled = NO;

            break;
        case 1:
            cell.titleLabel.text = @"课程结束时间";
            cell.placeHoldImageView.hidden = YES;
            cell.textFeild.hidden = YES;
            cell.textFeild.userInteractionEnabled = NO;
            
            break;
        case 2:
            cell.titleLabel.text = @"上课地方";
            cell.placeHoldImageView.hidden = YES;
            cell.textFeild.hidden = YES;
            cell.textFeild.userInteractionEnabled = NO;


            break;
        case 3:
            cell.titleLabel.text = @"详细地址";
            cell.placeHoldImageView.hidden = YES;
            cell.textFeild.hidden = NO;
            cell.textFeild.placeholder = @"请输入具体位置";

            break;
        case 4:
            cell.titleLabel.text = @"课程价格";
            cell.placeHoldImageView.hidden = YES;
            cell.textFeild.hidden = NO;
            cell.textFeild.placeholder = @"请输入金额";
            break;
    }
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row < 2){
        self.selectedIndexPath = indexPath;
    }
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    WS(weakSelf)
    if(indexPath.row == 0 || indexPath.row == 1){
        [self.view bringSubviewToFront:self.datePicker];
        if(self.pickerView.My_Y < LYScreenHeight){
            [UIView animateWithDuration:0.5 animations:^{
                [weakSelf.pickerView setMy_Y:LYScreenHeight];
            }completion:^(BOOL finished) {
                [UIView animateWithDuration:.05 animations:^{
                    [weakSelf.datePicker setMy_Y:LYScreenHeight - weakSelf.datePickerHeight - 64];
                }];
            }];

            
        }else{
            [UIView animateWithDuration:.05 animations:^{
                [weakSelf.datePicker setMy_Y:LYScreenHeight - weakSelf.datePickerHeight - 64];
            }];
        }
        
        
        NSString *string = [[NSDate date] stringWithFormat:[NSDate dateFormatString]];
        LYSelectedClassInfoTableViewCell * cell = [self.tableView cellForRowAtIndexPath:self.selectedIndexPath];
        cell.placeHoldImageView.hidden = YES;
        cell.textFeild.text = string;
        cell.textFeild.hidden = NO;
        
        if (indexPath.row == 0){
            self.firstDate = [NSDate date];
        }else{
            self.secondDate = [NSDate date];
        }
        
    }else if (indexPath.row == 2){
        [self.view bringSubviewToFront:self.pickerView];
        if(self.datePicker.My_Y < LYScreenHeight){
            [UIView animateWithDuration:0.5 animations:^{
                [weakSelf.datePicker setMy_Y:LYScreenHeight];
            }completion:^(BOOL finished) {
                
                [UIView animateWithDuration:.05 animations:^{
                    [weakSelf.pickerView setMy_Y:LYScreenHeight - weakSelf.datePickerHeight - 64];
                }];
                
            }];

            LYSelectedClassInfoTableViewCell * cell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:2 inSection:0]];
            cell.placeHoldImageView.hidden = YES;
            cell.textFeild.text = [self.Provinces firstObject];
            cell.textFeild.hidden = NO;

        }else{
            [UIView animateWithDuration:.05 animations:^{
                [weakSelf.pickerView setMy_Y:LYScreenHeight - weakSelf.datePickerHeight - 64];
                
            }];
        }

    }else{
        
        LYSelectedClassInfoTableViewCell * cell = [self.tableView cellForRowAtIndexPath:indexPath];
        [UIView animateWithDuration:.05 animations:^{
            [weakSelf.datePicker setMy_Y:LYScreenHeight - weakSelf.datePickerHeight - 64];
        }completion:^(BOOL finished) {
            [cell.textFeild becomeFirstResponder];
        }];
    }
    

    
    
}

- (UIDatePicker *)datePicker{
    if(!_datePicker){
        _datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0,LYScreenHeight, LYScreenWidth, 200)];
        _datePicker.datePickerMode = UIDatePickerModeDate;
        [_datePicker addTarget:self action:@selector(dateChanged:) forControlEvents:UIControlEventValueChanged ];
    }
    
    return _datePicker;
}
-(void)dateChanged:(UIDatePicker*)sender{
    
    if(self.selectedIndexPath.row == 0){
        self.firstDate = sender.date;
    }else{
        self.secondDate = self.secondDate;
    }
    
    NSDate* date = sender.date;
   NSString *string = [date stringWithFormat:[NSDate dateFormatString]];
    
   LYSelectedClassInfoTableViewCell * cell = [self.tableView cellForRowAtIndexPath:self.selectedIndexPath];
    cell.placeHoldImageView.hidden = YES;
    cell.textFeild.text = string;
    cell.textFeild.hidden = NO;
    
//    NSString *dateFormatString =
    
    /*添加你自己响应代码*/
}

- (UIPickerView *)pickerView{
    
    if(!_pickerView){
        _pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, LYScreenHeight, LYScreenWidth, 200)];
        _pickerView.dataSource = self;
        _pickerView.delegate = self;
        
    }
    return _pickerView;
}

#pragma mark - delegate

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    
    return 3;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    
    switch (component) {
        case 0:
        {
            NSArray *provinces = [LYCityDataDBTool getProvinces];
            return provinces.count;
        }
            break;
        case 1:
        {
            NSArray *cityArray = [LYCityDataDBTool getSubAreaWithName:self.selectedProvince];
            return cityArray.count;
            
        }
            break;
        case 2:
        {
            NSArray *areaArray = [LYCityDataDBTool getSubAreaWithName:self.selectedCity];
            return areaArray.count;
        }
            break;
    }
    return 0;
}
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
    
    return LYScreenWidth / 3;
}
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component {
    
    return 40;
}

- (nullable NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    
//    return @"hao";
    
    NSArray *provinces = [LYCityDataDBTool getProvinces];
    self.Provinces = [LYCityDataDBTool getProvinces];
    
    NSString *title = nil;
    switch (component) {
        case 0:
        {
            title = self.Provinces[row];
        }
            break;
        case 1:
        {
            
            NSArray *cityArray = [LYCityDataDBTool getSubAreaWithName:self.selectedProvince];
            title = cityArray[row];

        }
            break;
        case 2:
        {
            NSArray *areaArray = [LYCityDataDBTool getSubAreaWithName:self.selectedCity];
            title = areaArray[row];
        }
            break;

    }
    
    return title;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    

    
    NSArray *provinces = [LYCityDataDBTool getProvinces];
    if ([self.pickerView selectedRowInComponent:0] < provinces.count){
        self.selectedProvince = provinces[[self.pickerView selectedRowInComponent:0]];
    }else{
        self.selectedProvince = nil;
    }
    NSArray *citys = [LYCityDataDBTool getSubAreaWithName:self.selectedProvince];
    
    if ([self.pickerView selectedRowInComponent:1] < citys.count){
        self.selectedCity = citys[[self.pickerView selectedRowInComponent:1]];
    }else{
        self.selectedCity = nil;
    }
    NSArray *areas = [LYCityDataDBTool getSubAreaWithName:self.selectedCity];
    
    if ([self.pickerView selectedRowInComponent:2] < areas.count){
        self.selectedArea = areas[[self.pickerView selectedRowInComponent:2]];
    }else{
        self.selectedArea = nil;
    }



    LYSelectedClassInfoTableViewCell * cell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:2 inSection:0]];
    cell.textFeild.text = [NSString stringWithFormat:@"%@%@%@",    LYNoNullString(self.selectedProvince), LYNoNullString(self.selectedCity),LYNoNullString(self.selectedArea)];
    
    
    
    [self.pickerView reloadAllComponents];
    cell.placeHoldImageView.hidden = YES;
    cell.textFeild.hidden = NO;
    
     NSString *areaID = [NSString stringWithFormat:@"%zd", [LYCityDataDBTool getIDWithName:self.selectedArea]];
    
    self.selectedAreaId = areaID;
}




@end
